//index.js
//获取应用实例
const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {
      avatarUrl: 'https://img2.woyaogexing.com/2021/04/10/48aab20bfcce45269a37ac319c1a9275!400x400.jpeg'
    },
    hasUserInfo: false,
    canIUse: true,
    isLogin: 'no', //是否登录
    userName: '', //用户名
  },

  NavChange(e) {
    this.setData({
      PageCur: e.currentTarget.dataset.cur
    })
  },
  //按钮点击跳转
  gotitle: function (param) {
    if(this.data.isLogin == 'is'){
      wx.navigateTo({
        url: '/pages/basics/title/title',
      })
    }else {
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
  },
  gosubject: function (param) {
    wx.navigateTo({
      url: '/pages/basics/subject/subject',
    })
  },
  //去个人中心
  gofile: function (param) {
    if(this.data.isLogin == 'is'){
      wx.navigateTo({
        url: '/pages/basics/file/file',
      });
    }else {
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
  },
  //错题
  gocollect: function (param) {
    wx.navigateTo({
      url: '/pages/basics/collect/collect',
    })
  },
  onLoad: function () {
    
  },
  setgk: function () {
    let promis = this.settime('-12-26')
    let that = this
    promis.then((arr) => {
      that.setData({
        gk_year: arr[0],
        gk_days: arr[1]
      });
    });
  },
  // 公共API
  // @params 传入节日日期的str，例如'-10-1','-12-25','-1-1'等
  // @return resolve()回调的是个数组
  // 数组第一个参数返回的是'今'或者'明'这个字符串，第二个参数返回的是还剩多少天
  settime: function (str) {
    let promis = new Promise((resolve, reject) => {

      // 获取时间对象
      let dateObj = new Date()
      let year = dateObj.getFullYear()
      let month = dateObj.getMonth()
      let day = dateObj.getDate()

      // 求当前日期和时间的时间戳
      // 这里需要注意的是，利用new Date().getMonth()得到的是0-11的数值
      // 而用new Date('year-month-day')初始化求今天0点0分0秒时的Month
      // 需要传入的是1-12的,也就是month要+1

      let now = new Date()
      let target = new Date(year + str) // 目标日期

      // 先保存起来，后续还会用
      let nowtime = now.getTime() // 当前日期时间戳
      let sjc = nowtime - target.getTime() // 时间差

      // 回调的2个参数，会组成数组传入回调函数中
      // 这2个信息会直接赋值显示到页面中
      let theyear = '今'
      let thedays = 0

      if (sjc < 0) {
        // 还未过今年某个节日
        theyear = '今'
        thedays = Math.floor(Math.abs(sjc / (24 * 60 * 60 * 1000)))

      } else if (sjc > 0) {
        // 已经过了今年某个节日
        let mn = year - 0 + 1
        let mntarget = new Date(mn + str)
        let sjc2 = mntarget.getTime() - nowtime
        theyear = '明'
        thedays = Math.floor(sjc2 / (24 * 60 * 60 * 1000))

      } else {
        // 一年的节日期间
        theyear = '今'
        thedays = 0
      }
      let arr = [theyear, thedays]
      resolve(arr)
    })
    return promis
  },
  //【去登录】
  toLoginFn(){
    wx.navigateTo({
      url: '/pages/login/login',
    });
  },
  //获取用户数据
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShow() {
    if(wx.getStorageSync('userId')) {
      this.setData({
        isLogin: 'is',
        canIUse: false,
        userName: wx.getStorageSync('userName')
      });
    }
    this.setgk(); // 考研日期
  }
})